<?php
/*
 * @Author: 天羽落星辰
 * @Date: 2020-07-17 15:04:53
 * @LastEditors: 天羽落星辰
 * @LastEditTime: 2020-09-30 18:00:23
 * @Description: file content   
 */

namespace app\admin\controller;

use think\Db;
use app\common\controller\Backend;
use app\admin\model\QianbaoSysetmLog;
use app\common\model\User;
/**
 * 1.05 用户合约订单管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;
    }

    /**
     * 激活正常合约
     *
     * @return void
     */
    public function give($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));

        if ($row['give'] == 0) {
            $this->error('不需要再激活，请刷新查看');
        }

        $bls_usdt = config('site.bls_usdt');
        Db::startTrans();
        try {
            $a1 = $this->model->update(['id' => $ids, 'give' => 0, 'bls' => ($row['price'] * $bls_usdt)]);
            $user = db('user')->field(true)->where('id',$row['uid'])->find();
            // $level = $this->users_vip($user['order_sum'] + $row['price']);
            // 直推奖
            // $zhitui = zhitui($user,$row['price']);
            $a2 = db('user')->where('id', $row['uid'])->inc('bls_frozen', ($row['price'] * $bls_usdt))->inc('order_sum', $row['price'])->update();
            // if($zhitui){
            //     $qianbaosysetmlog = new QianbaoSysetmLog;
            //     $users = new User;
                
            //     if(!$qianbaosysetmlog->save($zhitui['qian_log'], false)){
            //         throw new \Exception("接单失败",1);
            //     }
            //     if(!$users->isUpdate()->save($zhitui['user'])){
            //         throw new \Exception("接单失败",2);
            //     }
            // }
            
            if (!$a1 && !$a2) {
                throw new \Exception("激活失败", 1);
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $th) {
            Db::rollback();
            // $this->error($th->getCode());
            $this->error($th->getMessage());
        }

        $this->success('激活成功');
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    public function users_vip($order_sum = null)
    {
        $level = 0;
        if ($order_sum >= 200 && $order_sum < 500) {
            $level =  1;
        } elseif ($order_sum >= 500 && $order_sum < 1000) {
            $level =  2;
            $user->level = 2;
        } elseif ($order_sum >= 1000 && $order_sum < 3000) {
            $level =  3;
        } elseif ($order_sum >= 3000 && $order_sum < 5000) {
            $level =  4;
        } elseif ($order_sum >= 5000) {
            $level =  5;
        } else {
            $level =  6;
        }
        return $level;
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    $count += $v->delete();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success('删除成功');
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }
}
